<?php
require_once('combined_functions.php');

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Register New User</title>
</head>
<body>

<?php
//start session
session_start();

//shortcut variable names
$email=$_POST['email'];
$username=$_POST['username'];
$passwd=$_POST['passwd'];
$passwd2=$_POST['passwd2'];

try
{
	//check forms filled in
	if(!filled_out($_POST))
	{
		throw new Exception('You have not filled out the form out correctly - please go back and try again');
	}
	//email address not valid
	if(!valid_email($email))
	{
		throw new Exception('That is not a valid email address. Please go back and try again.');
	}
	//passwords not the same
	if($passwd !=$passwd2)
	{
		throw new Exception('The passwords you entered do not match- please go back and try again.');
	}
	//check password length is ok
	//ok if username truncates, but passwords will get
	//munged if they too long
	if ((strlen($passwd)<6) || (strlen($passwd)>16))
	{
		throw new Exception('Your password must be between 6 and 16 characters. Please go back and try again.');
	}
	//attempt to register
	//this function can also throw and exception
	register($username, $email, $passwd);
	
	//register session variable
	$_SESSION['valid_user']= $username;
	
	
	//provide link to registered user page
	echo 'Your registration was successful.  Go to the members page';
    do_html_url('registered_members.php', 'Go to members page');
}
catch(Exception $e)
{
	echo $e->getMessage();
}
?>
</body>
</html>